//student页面      /studata获取数据
import {param2Obj} from '../utils/paramsUrl'
import Mock from 'mockjs'
var List = [] //保存所有数据

for(var i=0;i<400;i++){
    var {phone} = Mock.mock({phone:/^[1]([3-9])[0-9]{9}$/})
    List.push({
        id:Mock.mock('@id'),//用户ID
        name:Mock.mock('@cname'),//微信昵称
        phone:phone,      //会员手机
        money: Mock.mock('@integer(1000, 3000)'),    //消费金额
        num: Mock.mock('@integer(1, 10)'),             //订单数量
        type:Mock.mock('@integer(0, 3)'),//             //会员等级
        paytime:Mock.mock('@datetime()'),//上次消费时间
       
    })
}

//抛出一对象
export default{
    //对应属性 获取数据
    getData:config=>{
        const { page,pageSize,searchVal } = param2Obj(config.url)
        console.log(page,pageSize);
        //1.搜索
        var newListarr = List.filter(item=>{
            return item.name.indexOf(searchVal) != -1
        })
        //2.分页
        //拿到 page = 1,pageSize = 100
        //(page-1)*pageSize <= index < page * pageSize
        var newList = newListarr.filter((item,index)=>{
            return(page-1) * pageSize <= index && index< page * pageSize
        })




        // List = newList
        return{
            code:200,
            msg:'查询成功',
            data:newList,
            total:newListarr.length  //List.length
        }
    },
    deleteData:config=>{
        console.log(config);
        const {id} = JSON.parse(config.body)
        // const id = JSON.parse(config.body).id
        for(var i=0;i<List.length;i++){
            if(List[i].id == id){
                List.splice(i,1)
            }
        }
        return{
            code:200,
            msg:'删除成功',
            data:null

        }
    },
    updateData:config=>{
        const {id,name,phone,money,num,type,paytime} = JSON.parse(config.body)
        for(var i=0;i<List.length;i++){
            if(List[i].id === id){
                List[i].name = name
                List[i].phone = phone
                List[i].money = money
                List[i].num = num
                List[i].type = type
                List[i].paytime = Mock.mock('@datetime()')
            }
        }
        return{
            code:200,
            msg:'修改成功',
            data:null
        }
    },
    dateData:config=>{
        const {ids} = JSON.parse(config.body)
        // console.log(ids)
        // console.log(config)
        var newList = List.filter(item=>{
            return ids.indexOf(item.id) == -1
        })
        List = newList
        // console.log(List)
        return{
            code:200,
            msg:'删除成功',
            data:null
        }
    }

}